package 人事管理系统.Connect;

import java.sql.*;
import java.net.InetAddress;
import javax.swing.*;
import java.net.UnknownHostException;
public class Accounts_Database {
    private String userName;
    private String userPassword;
    private  String ip=this.getIp();
    private String url="jdbc:mysql://"+ip+":3306/人事管理系统?useOldAliasMetadataBehavior=true";
    //获取ip
    public String getIp(){
        InetAddress addr = null;
        try {
            addr = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        System.out.println("Local HostAddress: "+addr.getHostAddress());
        String hostname = addr.getHostName();
        System.out.println("Local host name: "+hostname);
        return addr.getHostAddress();
    }
    //构造函数
    public Accounts_Database() {
    }

    public Accounts_Database(String userName, String userPassword) {
        this.userName = userName;
        this.userPassword = userPassword;
    }
    //get and set
    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserPassword() {
        return userPassword;
    }

    public void setUserPassword(String userPassword) {
        this.userPassword = userPassword;
    }
    /**
     * @return************************************************************************************/
//判断密码是否正确
    public boolean checkAccout() throws ClassNotFoundException, SQLException {
        //1.导入jar包
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2.注册驱动
        Connection con= DriverManager.getConnection(url,"root","020214YxY");
        //3.获取连接
        Statement stat=con.createStatement();
        //4.获取执行者对象

        //5.执行sql语句，并接收结果
        String sql="SELECT * FROM accounts";
        ResultSet rs = stat.executeQuery(sql);

        //6.处理结果
        boolean flag=false;
        while(rs.next()){
            //  System.out.println(rs.getString("userName"));
            //  System.out.println(rs.getString("userPassword"));
            String username=rs.getString("userName");
            String userpasswod=rs.getString("userPassword");
            //   System.out.println(username+"\t"+userpasswod);
            if(username.equals(this.userName))
            {
                //System.out.print("账号匹配");
                if(userpasswod.equals(this.userPassword)) {
                    System.out.println("密码匹配，登录成功！");
                    flag=true;
                    rs.close();
                    stat.close();
                    con.close();
                    return true;
                }
            }
        }
        if(!flag) {
            System.out.println("账号或密码输入错误！");
            new logindefeat();
        }
        //7.释放资源
        rs.close();
        stat.close();
        con.close();

        return false;
    }
    /*******************************************************************************/
//插入创建账户操作
    public void CreatAccount() throws ClassNotFoundException, SQLException {
        //1.导入jar包
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2.注册驱动
        Connection con= DriverManager.getConnection(url,"root","020214YxY");
        //3.获取连接
        Statement stat=con.createStatement();
        //4.获取执行者对象
        String username=this.userName;
        String userpassword=this.userPassword;
        //5.执行sql语句，并接收结果
        String sql="INSERT INTO accounts (userName,userPassword) values (?,?)";
        // 创建一个Statment对象
        PreparedStatement ps = con.prepareStatement(sql);
        // 为sql语句中第一个问号赋值
        ps.setString(1, this.userName);
        // 为sql语句中第二个问号赋值
        ps.setString(2, this.userPassword);
        ps.executeUpdate();
        //6.处理结果
        System.out.println("插入成功");
        new creatyes();
        //7.释放资源
        // rs.close();
        stat.close();
        con.close();

    }
    /*************************************************/
    //登陆失败弹窗
    class logindefeat{
        public logindefeat() {
            // 创建 JFrame 实例
            JFrame frame = new JFrame("系统提示");
            // Setting the width and height of frame
            frame.setSize(350, 200);
            frame.setLocation(500, 250);
            frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
            frame.setVisible(true);
            JLabel j = new JLabel("账号或密码错误，请重新输入或注册");
            frame.add(j);

        }}
    /*******************************************/
    //注册成功
    //登陆失败弹窗
    class creatyes{
        public creatyes() {
            // 创建 JFrame 实例
            JFrame frame = new JFrame("系统提示");
            // Setting the width and height of frame
            frame.setSize(350, 200);
            frame.setLocation(500, 250);
            frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
            frame.setVisible(true);
            JLabel j = new JLabel("注册成功");
            frame.add(j);

        }}

}